###### Session Information ######
'''
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.3
'''
####################################

#install.packages('lme4')
#install.packages("glmmTMB")
#install.packages("MuMIn")
#install.packages("devtools")
library("devtools") #version 2.4.5
devtools::install_local(".././lmSupport_2.9.13.tar.gz")
#install.packages("Matrix", dependencies = TRUE)
library("Matrix") #version 1.6.3
library("tidyr") #version 1.3.0
library("ggplot2") #version 3.4.3
library("dplyr") #version 1.1.4
library("lmSupport") #version 2.9.13
library("ggpubr") #version 0.6.0
library('readxl') #version 1.4.3
library("rstatix") #version 0.7.2
library("lme4") #version  1.1.35.1
library("glmmTMB") #version 1.1.8
library("MuMIn") #version 1.47.5

'''
R2GLMER<-function(model){
  
  # stopifnot((class(model)[1] == "lmerMod") | (class(model)[1] == "glmerMod"))
  
  # Calculated following Nakagawa & Schielzeth (2013) MEE
  VarF <- var(as.vector(fixef(model) %*% t(getME(model,"X"))))
  # Conditional R2 for the full model
  cond.r2<-(VarF + sum(unlist(lapply(VarCorr(model),function(x) return(x[1])))))/
    (VarF + sum(unlist(lapply(VarCorr(model),function(x) return(x[1])))) + 
       (attr(VarCorr(model), "sc")^2))
  
  # Marginal R2 for the fixed effects
  marg.r2<-VarF/(VarF + 
                   sum(unlist(lapply(VarCorr(model),function(x) return(x[1])))) +
                   (attr(VarCorr(model), "sc")^2))
  
  return(list(conditional=cond.r2,marginal=marg.r2))
  
}
'''



#import first sheet
dnew = read.csv(".././final_dataset_april6_2023.csv")
dnew

# cluster unique channel ids
dnew$channel_id <- dnew %>% dplyr::group_indices(channel_id) # of Observations: 4008
dnew$channel_id



########################
##Research Question 2 ##
########################

# Convert gender to an unordered factor
dnew$gender_or_neutral <- as.factor(dnew$gender_or_neutral)
dnew$gender_or_neutral <- relevel(dnew$gender_or_neutral, ref = "Gender-neutral words")

######## Regression_base  ########
######## DV 1: View count ########
### DV is engagement (View count), ### 
### IV is: {name = gender_or_neutral, categorical: gender-neutral words  (baseline) or gender words}, ### 
### CVs are: {moral strength scores} + {channel_id} + {platform: YouTube or TikTok} + {issue: Climate or Vaccine}

summary(m5<- glmmTMB(video_view_count ~ gender_or_neutral + care_strength + fairness_strength + loyalty_strength + authority_strength + sanctity_strength + liberty_strength + channel_id + platform + issue + (1|channel_id), family = "nbinom2"(link="log"), control = glmmTMBControl(parallel = 5), REML = FALSE, data = dnew))
r.squaredGLMM(m5)

######## Regression_base  ########
######## DV 2: Like count ########
### DV is engagement (Like count), ### 
### IV is: {name = gender_or_neutral, categorical: gender-neutral words  (baseline) or gender words}, ### 
### CVs are: {moral strength scores} + {channel_id} + {platform: YouTube or TikTok} + {issue: Climate or Vaccine}

summary(m6<- glmmTMB(video_like_count ~ gender_or_neutral + care_strength + fairness_strength + loyalty_strength + authority_strength + sanctity_strength + liberty_strength + channel_id + platform + issue + (1|channel_id), family = "nbinom2"(link="log"), control = glmmTMBControl(parallel = 5), REML = FALSE, data = dnew))
r.squaredGLMM(m6)
